package com.loong.news.handler;

import com.loong.common.redis.RedisTools;
import com.loong.common.utils.TransformUtils;
import com.loong.global.constant.RedisKey;
import com.loong.news.business.NewsCommentBusiness;
import com.loong.news.common.forehead.param.DeleteNewsCommentParam;
import com.loong.news.common.forehead.param.NewsCommentParam;
import com.loong.news.common.forehead.result.NewsCommentResult;
import com.loong.news.mapper.NewsCommentMapper;
import com.loong.news.model.NewsComment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description:新闻评论的业务操作层
 * @Author: 刘群
 * @Date: 2020/10/18 11:01
 */
@Service
public class NewsCommentHandler {

    @Resource
    private NewsCommentBusiness commentBusiness;

    @Resource
    private NewsCommentMapper commentMapper;

    /**
     * 新增新闻评论
     * @param param
     */
    public void saveNewsComment(NewsCommentParam param){
        param.setCreateTime(new Date());
        commentBusiness.save(TransformUtils.transform(param, NewsComment.class));
    }

    /**
     * 删除评论
     * @param param
     */
    @Transactional
    public void deleteNewsComment(DeleteNewsCommentParam param){
        //把自己的评论删除:
        commentMapper.deleteParentNewsComment(param.getId(),param.getUserId());
        //TODO 对于是否该把该评论下的所有回复也删除，这个之后再考虑
//        commentMapper.deleteChildNewsComment(param.getId());
    }

    /**
     * 获得新闻的评论：
     * @param newsId
     * @return
     */
    public List<NewsCommentResult> findNewsComment(Long newsId){
        //根据新闻id查询出该新闻所有的评论：
        List<NewsComment> commentList = commentMapper.findNewsCommentByNewsId(newsId);
        List<NewsCommentResult> allComment = TransformUtils.transformList(commentList, NewsCommentResult.class);

        Long num = 0L;
        Map<Long,NewsCommentResult> map = new HashMap<>();
        List<NewsCommentResult> result = new ArrayList<>();
        //遍历所有的评论
        for (NewsCommentResult comment : allComment){
            if (num.equals(comment.getParentId())){
                result.add(comment);//直接将父评论存放到集合中
            }
            map.put(comment.getId(),comment);//将所有评论都存放到map中
        }
        for (NewsCommentResult comment : allComment){
            if (!(num.equals(comment.getParentId()))){
                //该评论有父评论，则从Map中获取到该父评论
                NewsCommentResult parent = map.get(comment.getParentId());
                if (parent.getChildList() == null){
                    parent.setChildList(new ArrayList<>());
                }
                //将该评论添加到对应的父评论的集合中
                parent.getChildList().add(comment);
            }
        }
        return result;
    }

    /**
     * 点赞评论
     * @param id
     */
    public void addLikeNum(Long id){
        RedisTools.valueOperations().increment(RedisKey.NEWS_COMMENT.COMMENT_ID+id+RedisKey.NEWS_COMMENT.LIKE_NUM);
    }

    /**
     * 踩/举报评论
     * @param id
     */
    public void addUnLikeNum(Long id){
        RedisTools.valueOperations().increment(RedisKey.NEWS_COMMENT.COMMENT_ID+id+RedisKey.NEWS_COMMENT.UNLIKE_NUM);
    }

    /**
     * 更新新闻评论的点赞数和举报数：
     * @param id
     */
    public void updateLikeNumAndUnlikeNum(Long id){
        //从Redis中获取点赞数和举报数
        Integer likeNum = (Integer) RedisTools.valueOperations().get(RedisKey.NEWS_COMMENT.COMMENT_ID + id + RedisKey.NEWS_COMMENT.LIKE_NUM);
        Integer unLikeNum = (Integer) RedisTools.valueOperations().get(RedisKey.NEWS_COMMENT.COMMENT_ID + id + RedisKey.NEWS_COMMENT.UNLIKE_NUM);

        if (likeNum == null) likeNum = 0;
        if (unLikeNum == null) unLikeNum = 0;

        NewsCommentParam param = NewsCommentParam.builder()
                .id(id)
                .likeNum(likeNum.longValue())
                .unlikeNum(unLikeNum.longValue())
                .lastUpdateTime(new Date())
                .build();
        //更新数据库中新闻评论的点赞数和举报数：
        commentBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param,NewsComment.class));
    }
}
